package com.bw.z.mvp.view.video;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.z.R;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.view.BaseFragment;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.di.DaggerNewsComponent;
import com.bw.z.mvp.di.NewsModules;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.VideoNewsEntity;
import com.bw.z.mvp.presenter.NewsPresenter;
import com.bw.z.mvp.view.video.adapter.SmallVideoAdapter;
import com.bw.z.utils.ToastUtils;
import com.bw.z.widget.VideoNewsItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmallVideoFragment extends BaseFragment<NewsPresenter>
        implements NewsContract.NewView, OnRefreshLoadMoreListener, OnItemClickListener {


    private SmartRefreshLayout smallVideoSmart;
    private RecyclerView smallVideoRv;
    private GridLayoutManager manager;
    private SmallVideoAdapter adapter;
    private List<VideoNewsEntity.Values> list = new ArrayList<>();
    private int index = 0;

    @Override
    public int bindLayout() {
        return R.layout.fragment_small_video;
    }

    @Override
    public void initView() {
        smallVideoSmart = f(R.id.small_video_smart);
        smallVideoSmart.setEnableLoadMore(true);
        smallVideoSmart.setEnableRefresh(true);
        smallVideoSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        smallVideoSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        smallVideoSmart.setOnRefreshLoadMoreListener(this);
        smallVideoRv = f(R.id.small_video_rv);
        manager = new GridLayoutManager(getContext(), 2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        smallVideoRv.setLayoutManager(manager);
        smallVideoRv.addItemDecoration(new VideoNewsItemDecoration(getContext()));
        adapter = new SmallVideoAdapter();
        smallVideoRv.setAdapter(adapter);
        adapter.setOnItemClickListener(this::onItemClick);
    }

    @Override
    public void inject() {
        DaggerNewsComponent.builder().newsModules(new NewsModules(this)).build().injectSmallVideoNews(this);
    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("news_type", 0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues() == null) {
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewInstance(videoNewsEntity.getValues());
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues() == null) {
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewInstance(videoNewsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues() == null) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        list = adapter.getData();
        list.addAll(videoNewsEntity.getValues());
        adapter.setNewInstance(list);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("news_type", 0);
        newsMap.put("code", HttpCode.VIDEONEWSCODE);
        p.requestLoad(newsMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        if (list != null)
            list.clear();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("news_type", 0);
        newsMap.put("code", HttpCode.VIDEONEWSCODE);
        p.requestRefresh(newsMap);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ImageView small_item_cover_img = view.findViewById(R.id.small_item_cover_img);
        TextView small_item_title_tv = view.findViewById(R.id.small_item_title_tv);
        StandardGSYVideoPlayer small_item_gsy_player = view.findViewById(R.id.small_item_gsy_player);
        small_item_cover_img.setVisibility(View.GONE);
        small_item_title_tv.setVisibility(View.GONE);
        small_item_gsy_player.setVisibility(View.VISIBLE);
        small_item_gsy_player.setUp("http://ivi.bupt.edu.cn/hls/cctv6hd.m3u8", true, "");
        small_item_gsy_player.startPlayLogic();
    }
}